package org.lex.utils;

public enum Result {
	ERROR("Error"), SUCCESS("Success");

	private static String defaultErrorMessage = "Error";
	private static String defaultSuccessMessage = "Success";

	private String lastMessage = null;

	private Result(String message) {
		this.setLastMessage(message);
	}

	// ==========================================
	public String getLastMessage() {
		return this.lastMessage;
	}

	public Result setLastMessage(String message) {
		this.lastMessage = message;
		return this;
	}

	public boolean isError() {
		return this == ERROR;
	}

	public boolean isSuccess() {
		return this == SUCCESS;
	}

	public String toString() {
		return this.name() + ": " + this.lastMessage;
	}

	// ======================================
	public static String getLastErrorMessage() {
		return Result.ERROR.getLastMessage();
	}

	public static Result error(String message) {
		return Result.ERROR.setLastMessage(message);
	}

	public static Result error() {
		return Result.error(defaultErrorMessage);
	}

	public static Result error(Throwable e) {
		return Result.error(e.getLocalizedMessage());
	}

	// ==========================================
	public static String getLastSuccessMessage() {
		return Result.SUCCESS.getLastMessage();
	}

	public static Result success(String message) {
		return Result.SUCCESS.setLastMessage(message);
	}

	public static Result success() {
		return Result.success(defaultSuccessMessage);
	}
}
